﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_MyUserControls_TH_ChiTietQuangCao : System.Web.UI.UserControl
{
    private DataSet objDataSet;
    private int id;
    protected void Page_Load(object sender, EventArgs e)
    {
        id = int.Parse(Request.QueryString["id"].ToString());

        if (!IsPostBack)
        {
            // Fill controls with data
            LoadChiTietQuangCao();
        }
    }

    public void LoadChiTietQuangCao()
    {
        QuangCaoBUS objQuangCao = new QuangCaoBUS(TGDDConfiguration.DbConnectionString);
        objDataSet = objQuangCao.SelectQuangCao(id);
        DataRow row = objDataSet.Tables[0].Rows[0];
        ListItem item = new ListItem(row["ViTri"].ToString(), row["ViTri"].ToString());
        lstViTri.SelectedIndex = lstViTri.Items.IndexOf(item);
        txtGia.Text = row["Gia"].ToString();
        txtTenKhachHang.Text = row["TenKhachHang"].ToString();
        txtWebsite.Text = row["Website"].ToString();
        txtNgayBatDau.Text = row["NgayBatDau"].ToString();
        txtNgayKetThuc.Text = row["NgayKetthuc"].ToString();
        imgHinhMinhHoa.ImageUrl = Request.ApplicationPath + "/images/quangcao/" +
                row["HinhMinhHoa"].ToString();
    }

    private void MessageBox(string msg)
    {
        Page.Controls.Add(new LiteralControl(
         "<script language='javascript'> window.alert('" + msg.Replace("'", "\\'") + "');</script>"));
    }

    protected void lkbtnCapNhat_Click(object sender, EventArgs e)
    {
        QuangCaoBUS objQuangCao = new QuangCaoBUS(TGDDConfiguration.DbConnectionString);
        QuangCaoDTO qcDto = new QuangCaoDTO();
        qcDto.Id = id;
        qcDto.ViTri = int.Parse(lstViTri.SelectedValue.ToString());
        qcDto.HinhMinhHoa = fileUploadHinhAnh.FileName;
        qcDto.Gia = long.Parse(txtGia.Text);
        qcDto.TenKhachHang = txtTenKhachHang.Text;
        qcDto.WebSite = txtWebsite.Text;
        qcDto.NgayBatDau = DateTime.Parse(txtNgayBatDau.Text);
        qcDto.NgayKetThuc = DateTime.Parse(txtNgayKetThuc.Text);
        int type = 0;
        if (!fileUploadHinhAnh.FileName.Equals(""))
            type = 1;
        if (objQuangCao.UpdateQuangCao(qcDto, type))
        {
            if (type == 1)
            {
                string strBaseLocation = Server.MapPath("..") + "\\images\\quangcao\\" + fileUploadHinhAnh.FileName;

                if (fileUploadHinhAnh.PostedFile != null)
                {
                    fileUploadHinhAnh.PostedFile.SaveAs(strBaseLocation);
                }
            }
            MessageBox("Cập nhật thành công");
        }
        else
        {
            MessageBox("Cập nhật thất bại");
        }
    }
}
